package com.qph.base.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.qph.base.cache.Caches;
import com.qph.base.dao.BaseDao;
import com.qph.base.exception.BusinessException;
import com.qph.base.model.Pagination;
import com.qph.base.model.SystemContext;
import com.qph.base.service.BaseService;
import com.qph.utils.base.StringUtil;

/**
 * 通用Service实现<br>
 * 创建时间：2012-5-3<br>
 * 说明：daoBase注入方式
 * 修改人：Penghui.Qu
 * 修改时间：2012-08-02
 * @param <T>
 *            model
 * @param <E>
 *            dao
 * 
 */
public class BaseServiceImpl<T, E extends BaseDao<T>> implements BaseService<T> , Caches {

	protected E dao;
	

	/**
	 * 用于spring注入
	 * @param dao
	 */
	public void setDao(E dao) {
		this.dao = dao;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.general.common.service.BaseService#insert(java.lang.Object)
	 */
	public boolean insert(T t) throws BusinessException{
		int result = dao.insert(t);
		return result > 0;
	}

	/*
	 * (non-Javadoc)
	 * @see com.general.common.service.BaseService#update(java.lang.Object)
	 */
	public boolean update(T t){
		int result = dao.update(t);
		return result > 0;
	}

	/*
	 * (non-Javadoc)
	 * @see com.general.common.service.BaseService#getModel(java.lang.String)
	 */
	public T getModel(String id){
		return dao.getModel(id);
	}

	/*
	 * (non-Javadoc)
	 * @see com.general.common.service.BaseService#getModel(java.lang.Object)
	 */
	public T getModel(T t){
		return dao.getModel(t);
	}

	/*
	 * (non-Javadoc)
	 * @see com.general.common.service.BaseService#batchDelete(java.util.Map)
	 */
	public boolean batchDelete(Map<String, Object> map){
		int result = dao.batchDelete(map);
		return result > 0;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.general.common.service.BaseService#batchDelete(java.util.List)
	 */
	public boolean batchDelete(List<?> list){
		int result = dao.batchDelete(list);
		return result > 0;
	}

	/*
	 * (non-Javadoc)
	 * @see com.general.common.service.BaseService#batchUpdate(java.util.Map)
	 */
	public boolean batchUpdate(Map<String, Object> map){
		int result = dao.batchUpdate(map);
		return result > 0;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.general.common.service.BaseService#getPagedList(java.lang.Object)
	 */
	public Pagination getPagedList(T t ){
		
		List<T> list = dao.getPagedList(t);
		Pagination pagination = new Pagination();
		pagination.setRows(list);
		pagination.setTotal(SystemContext.getTotal());
		return pagination;
	}

	/*
	 * (non-Javadoc)
	 * @see com.general.common.service.BaseService#getModelList(java.lang.Object)
	 */
	public List<T> getModelList(T t){

		return dao.getModelList(t);
	}

	/*
	 * (non-Javadoc)
	 * @see com.general.common.service.BaseService#getModelList(java.lang.String[])
	 */
	public List<T> getModelList(String... str){

		return dao.getModelList(str);
	}

	/*
	 * (non-Javadoc)
	 * @see com.general.common.service.BaseService#getCount(java.lang.Object)
	 */
	public int getCount(T t){
		return dao.getCount(t);
	}

	/*
	 * (non-Javadoc)
	 * @see com.general.common.service.BaseService#getModelListByScope(java.lang.Object)
	 */
	public List<T> getModelListByScope(T t) {
		 
		return dao.getModelListByScope(t);
	}

	/*
	 * (non-Javadoc)
	 * @see com.general.common.service.BaseService#getPagedByScope(java.lang.Object)
	 */
	public List<T> getPagedByScope(T t) {
		
		return dao.getPagedByScope(t);
	}

	
	/*
	 * (non-Javadoc)
	 * @see com.general.common.service.BaseService#delete(java.lang.String)
	 */
	public int delete(String ids) {
		
		if (StringUtil.isEmpty(ids)){
			return 0; 
		}
		
		String[] idArr = ids.split(",");
		List<String> list = new ArrayList<String>(Arrays.asList(idArr));
		
		return dao.batchDelete(list);
	}

	
}
